﻿using System;
using System.Collections.Generic;

namespace Eflong.Monitor
{
    /// <summary>
    /// 5 分钟统计记录
    /// </summary>
    public class Record5Min
    {
        public Record5Min( )
        {
            list_StatValue = new List<StatValue>( );
            statValue = new StatValue( );
            avgDayReport = 0;

            bDayReport = false;
            b5MinInit = false;
        }

        public StatValue statValue;
        private List<StatValue> list_StatValue;

        /// <summary>
        /// 加入统计的数据
        /// </summary>
        /// <param name="v"></param>
        public void AddValue( float v , bool commNormal )
        {
            DateTime t = DateTime.Now;

            // 按每5分钟时间段,分别进行统计,并把统计结果记录下来
            if ( t.Minute % 5 == 0 && t.Second < 10 )
            {
                if ( !b5MinInit )
                {
                    b5MinInit = true;

                    // 克隆一个当前的统计
                    StatValue item = statValue.Clone( );
                    if ( item != null )
                    {
                        if ( commNormal )
                        {
                            // 重新初始化,开始新的统计

                            statValue.ReInit( v );
                        }
                        else
                        {
                            // 通讯不正常时,进行初始化

                            statValue.ReInit( 0 );
                        }

                        // 把上次统计的结果加入到 List
                        list_StatValue.Add( item );
                        if ( list_StatValue.Count > 24 * 60 / 5 )
                        {
                            // 超过24小时的统计值,清除掉

                            list_StatValue.RemoveAt( 0 );
                        }
                    }
                    else
                    {
                        // 还未正式开始统计

                        if ( commNormal )
                        {
                            statValue.Changing( v );
                        }
                    }
                }
                else
                {
                    if ( commNormal )
                    {
                        statValue.Changing( v );
                    }
                }
            }
            else
            {
                b5MinInit = false;

                // 继续统计
                if ( commNormal )
                {
                    statValue.Changing( v );
                }
            }

            if ( t.Hour >= 8 )
            {
                if ( !bDayReport )
                {
                    bDayReport = true;

                    // 每天早上8点，以后出一次报表值

                    float sumAvg = 0;
                    foreach ( var r in list_StatValue )
                    {
                        sumAvg += r.avg;
                    }
                    float n = list_StatValue.Count;
                    if ( n > 0 )
                    {
                        avgDayReport = sumAvg / n;
                    }
                    else
                    {
                        avgDayReport = 0;
                    }
                }
            }
            else
            {
                bDayReport = false;
            }
        }

        private bool bDayReport;

        // 每日平均值

        public float avgDayReport;

        private bool b5MinInit;
    }
}